/**
 * Filename: DoLikeService.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/18 20:10
 */
package com.github.postsanf.yinian.service;

import android.content.Intent;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNCommonResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import java.util.LinkedHashMap;

/**
 * 点赞处理服务
 */
public class DoLikeService extends BaseIntentService {

    public DoLikeService() {
        super("DoLikeService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            switch(action){
                // 获取用户动态
                case ACTIONs.actionDoLick:
                    String     eventid   = intent.getExtras().getString(ACTIONs.aLoadId);
                    String     type      = intent.getExtras().getString(ACTIONs.aNeedType);
                    doELike(eventid,type);
                    break;
            }
        }
    }

    // 处理点赞
    public void doELike(String eventid,String type){
        LinkedHashMap<String,String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID,String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_EVENT_IDD,eventid);
        mParam.put(CommonConstants.YNS_INFO_TYPE, type);
        final StringRequest req = new StringRequest(getString(R.string.BASE_URL)+getString(R.string.ynLikeAndUnlike))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true){
                    @Override
                    public void onSuccess(String s, Response<String> response){
                        YNCommonResponse resIsLike = gson.fromJson(s, YNCommonResponse.class);
                        if (ReqUtils.isSuccess(resIsLike.getCode())){
                        } else {
                            showToast("操作失败");
                        }
                    }
                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}
